package com.tc.zigbee.entity.device.ha;

import com.tc.gateway.device.ha.OnoffOutput;
import com.tc.gateway.device.listener.OnoffListener;
import com.tc.zigbee.entity.Endpoint;
import com.tc.zigbee.entity.device.ZigbeeDevice;
import com.tc.zigbee.mt.MtCommand;
import com.tc.zigbee.mt.RequestQueue;
import com.tc.zigbee.mt.ZigbeeManager;
import com.tc.zigbee.mt.request.RequestFactory;
import com.tc.zigbee.zcl.ZclCommandCreator;
import com.tc.zigbee.zcl.ZclConstants;

public class ZigbeeOnoffOutput extends ZigbeeDevice<OnoffListener> implements OnoffOutput{

	public ZigbeeOnoffOutput(Endpoint endpoint,ZigbeeManager m) {
		super(endpoint,m);
		// TODO Auto-generated constructor stub
	}

	public void action() {
		toggle();

	}
	
	public void toggle(){

		byte[] zclcommand = ZclCommandCreator.toggle((byte)0);
		MtCommand mc = RequestFactory.MtAfDataRequest((byte)2,
				endpoint.getParent().getNwkAddress(), 
				//(short)1,
				endpoint.getEndpointId(), 
				(byte)manager.remoteControl.getEndpointId(), 
				ZclConstants.ZCL_CLUSTER_ID_GEN_ON_OFF, 
				(byte)0, 
				(byte)0, 
				(byte)2, 
				(byte)zclcommand.length, 
				zclcommand
				);
		RequestQueue.offer(mc);
		onoff = !onoff;
		for(OnoffListener l : this.listeners)
			l.onChange(onoff);
	}

	public void off() {
		// TODO Auto-generated method stub
		
	}

	public void on() {
		// TODO Auto-generated method stub
		
	}
	boolean onoff;
	@Override
	public boolean getOnOff() {
		// TODO Auto-generated method stub
		return onoff;
	}

}
